<template>
  <view class="page">
    <view class="search-bar">
      <view class="search-input">
        <input type="text" v-model="keywords" placeholder="请输入工号/姓名" />
        <image class="icon-search" src="/static/images/icon_search.png" mode=""></image>
        <view class="search-button" @click="search">搜索</view>
      </view>
    </view>
    <scroll-view class="scroll-list" scroll-y="true" @scrolltolower="loadMore">
      <view class="card-list">
        <view v-for="(item, index) in list" :key="index" class="card-item" @click="handleClick(item)">
          <view class="card-item-bd">
            <view class="card-title">{{item.name}}</view>
            <view class="card-desc">{{item.deptName}}</view>
          </view>
          <view v-if="hasPermission('misAssistantTalkCoach_delete')" class="card-item-ft">
            <view class="recovery" @click.stop="handleDelete(item)">回收</view>
          </view>
        </view>
      </view>
      <!-- <view class="uni-list" v-for="(item, index) in list" :key="index" style="margin-top: 5rpx;margin-bottom: 5rpx;"
        @click="handleClick(item)">
        <u-row gutter="16" style="margin-top: 20rpx;">
          <u-col span="12"
            style="word-break:break-all;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden;font-weight:bold">
            {{item.name}}[{{item.account}}]
          </u-col>
        </u-row>
        <u-row gutter="16" style="margin-top: 20rpx;margin-bottom: 10rpx;">
          <u-col span="6">
            <view style="text-align: left;color: gray;">
              {{item.deptName}}
            </view>
          </u-col>
          <u-col span="6" style="text-align: right;">
            <u-button size="mini" v-if="hasPermission('misAssistantTalkCoach_delete')" shape="circle" plain
              type="primary" @click="handleDelete(item)">回收</u-button>
          </u-col>
        </u-row>
      </view> -->
      <u-loadmore class="loadmore" :status="loadStatus"></u-loadmore>
    </scroll-view>
    <!-- <view class="btn-plus" @click="chooseStudent" v-if="hasPermission('misAssistantTalkCoach_add')">
      <u-icon name="plus-circle-fill" size="90" color="#3d87ff"></u-icon>
    </view> -->
    <view v-if="hasPermission('misAssistantTalkCoach_add')" class="add-btn" @click="chooseStudent">
      <image src="/static/images/icon_add_message.png" mode=""></image>
    </view>
    <JsPopup ref="studentRef" v-if="hasPermission('misAssistantTalkCoach_add')" :defaultVaule="model.userList"
      @submit="doSelected" :multiple="popup.multiple" :mode="popup.mode"
      action="/api/blade-system/user/user-list-by-dept-mobile"></JsPopup>
  </view>
</template>
<script>
  import $time from '@/common/time.js';
  import JsPopup from '@/components/js-popup/js-popup.vue'
  export default {
    components: {
      JsPopup
    },
    data() {
      return {
        keywords: '',
        query: {
          current: 1,
          size: 10
        },
        list: [],
        pages: 0,
        loadStatus: 'loadmore',
        options: [],
        headStyle: {
          fontWeight: 'bold',
          color: '#606266',
          margin: '16rpx'
        },
        statusList: [],
        swiperCurrent: 0,
        popup: {
          show: false,
          mode: "bottom",
          multiple: true,
        },
        model: {
          userList: [],

        },
      };
    },
    onLoad() {
      this.loadList();
    },
    computed: {

    },
    methods: {
      navTo(url) {
        uni.navigateTo({
          url: url
        });
      },
      search(value) {
        this.list = [];
        this.query.current = 0;
        this.loadList();
      },
      loadList() {
        this.query.keywords = this.keywords;
        this.$u.api.mis.misAssistantTalkCoach.list(this.query).then(res => {
          if (!res.data.records || res.data.records.length == 0) {
            this.loadStatus = "nomore";
            return;
          }
          res.data.records.forEach((item) => {
            item.show = false;
          })
          this.list = this.list.concat(res.data.records);
          this.pages = res.pages;
          this.query.current = res.data.current;
          this.query.size = res.data.size;
          this.loadStatus = "loadmore";
        });
      },
      loadMore() {
        this.loadStatus = "loading";
        setTimeout(() => {
          this.query.current += 1;
          this.loadList();
        }, 100);
      },
      handleClick(row) {
        this.navTo('form?id=' + row.id);
      },
      chooseStudent() {
        this.$refs.studentRef.setDefaultVal(this.model.userList);
        this.$refs.studentRef.open();
      },
      doSelected() {
        let userList = this.$refs.studentRef.getData();
        let userId = [];
        if (userList.length > 0) {
          let list = [];
          userList.forEach(item => {
            list.push(item.id);
          });
          uni.showLoading({
            title: '提交中....'
          })
          this.$u.api.mis.misAssistantTalkCoach.grantAssistant({
            userIds: list.join(",")
          }).then(res => {
            uni.hideLoading();
            uni.showModal({
              title: '提示',
              content: res.msg,
              showCancel: false,
              success: function() {
                if (res.code == 200) {
                  uni.navigateTo({
                    url: 'index'
                  });
                }
              }
            });
            this.$refs.studentRef.close();
            this.$refs.studentRef.clearAll();
          });
        } else {
          this.$refs.studentRef.close();
        }
      },
      handleDelete(row) {
        let self = this;
        uni.showModal({
          title: '提示',
          content: '确认要删除该数据吗？',
          showCancel: true,
          success: function(res2) {
            if (res2.confirm) {
              self.$u.api.mis.misAssistantTalkCoach.remove({
                ids: row.id
              }).then(res => {
                self.$u.toast('删除成功！');
                uni.navigateTo({
                  url: 'index'
                });
              });
            }
          }
        });
      },
    }
  };
</script>
<style lang="scss">
  .page {
    position: relative;
    background: #F7F7F7;
  }

  .search-bar {
    display: flex;
    align-items: center;
    padding: 16rpx 32rpx;
    background: #fff;
  }

  .search-input {
    position: relative;
    width: 100%;
    height: 64rpx;
    border-radius: 32rpx;
    background: #F7F7F7;
    overflow: hidden;

    input {
      width: 100%;
      height: 64rpx;
      border: 0;
      padding: 0 144rpx 0 64rpx;
      outline: 0;
      line-height: 64rpx;
      font-size: 24rpx;
      font-weight: 500;
      color: #999;
      background: transparent;
    }

    .icon-search {
      position: absolute;
      top: 21rpx;
      left: 21rpx;
      width: 22rpx;
      height: 22rpx;
    }

    .search-button {
      position: absolute;
      top: 0;
      right: 0;
      width: 144rpx;
      border-radius: 32rpx;
      line-height: 64rpx;
      text-align: center;
      font-size: 24rpx;
      font-weight: 700;
      color: #fff;
      background: #3989FC;
    }
  }

  .scroll-list {
    height: calc(100vh - var(--window-top) - var(--window-bottom) - 96rpx);

    .loadmore {
      padding: 32rpx;
    }
  }

  .card-list {
    display: flex;
    flex-direction: column;
    padding: 24rpx 24rpx 0;
  }

  .card-item {
    display: flex;
    border-radius: 8rpx;
    padding: 32rpx;
    margin-bottom: 24rpx;
    background: #fff;
    font-size: 28rpx;
    font-weight: 500;
    color: #ccc;
  }

  .card-item-bd {
    flex: 1;
    min-width: 0;
  }

  .card-item-ft {
    display: inline-flex;
    flex-direction: column;
    justify-content: flex-end;
  }

  .card-title {
    margin-bottom: 16rpx;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-size: 32rpx;
    font-weight: 700;
    color: #333;
    overflow: hidden;
  }

  .card-desc {
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }

  .recovery {
    display: inline-block;
    border-radius: 28rpx;
    padding: 0 24rpx;
    line-height: 56rpx;
    font-size: 24rpx;
    font-weight: 500;
    color: #3989FC;
    background: #DEEBFE;
  }
  
  .add-btn {
    position: absolute;
    right: 32rpx;
    bottom: 288rpx;
    z-index: 99;
    width: 88rpx;
    height: 88rpx;
  
    image {
      width: 88rpx;
      height: 88rpx;
      vertical-align: top;
    }
  }
</style>